import os
import subprocess
import sys

from log import WriteLog
from upgrade_scene import UpgradeScene


class RunUpgrade(UpgradeScene):
    def __init__(self, *args, **kwargs):
        super(RunUpgrade, self).__init__(*args, **kwargs)
        self.log = WriteLog()
        self.u_prefix_1 = kwargs['upgrade_user_prefix']
        self.u_prefix_2 = kwargs['upgrade_user_prefix'][:-3] + '_2_'

    def run_indirect_grey(self, befor_v, middle_v, after_v, low_path,
                          mid_path, high_path):
        self.up.befor_version = befor_v
        self.up.after_version = after_v
        self.log.print_log(f'灰度升级{befor_v}~{middle_v}~{after_v}')
        self.indirect_grey(self.u_prefix_1, self.u_prefix_2, low_path,
                           high_path, middle_v, mid_path)

    def run_indirect(self, befor_v, middle_v, after_v, low_path,
                     mid_path, high_path):
        self.up.befor_version = befor_v
        self.up.after_version = after_v
        self.log.print_log(f'就地升级{befor_v}~{middle_v}~{after_v}')
        self.indirect(self.u_prefix_1, self.u_prefix_2, low_path,
                      high_path, middle_v, mid_path)

    def run_indirect_grey_rollback(self, befor_v, middle_v, after_v, low_path,
                                   mid_path, high_path):
        self.up.befor_version = befor_v
        self.up.after_version = after_v
        self.log.print_log(
            f'灰度升级{befor_v}~{middle_v}~{after_v}再回滚到{middle_v}')
        self.indirect_grey_rollback(self.u_prefix_1, self.u_prefix_2,
                                    low_path, high_path, middle_v,
                                    mid_path)

    def run_indirect_grey_force_rollback(self, befor_v, middle_v, after_v,
                                         low_path, mid_path, high_path):
        self.up.befor_version = befor_v
        self.up.after_version = after_v
        self.log.print_log(
            f'灰度升级{befor_v}~{middle_v}~{after_v}再强制回滚到{middle_v}')
        self.indirect_grey_force_rollback(self.u_prefix_1, self.u_prefix_2,
                                          low_path, high_path, middle_v,
                                          mid_path)

    def run_indirect_rollback(self, befor_v, middle_v, after_v, low_path,
                              mid_path, high_path):
        self.up.befor_version = befor_v
        self.up.after_version = after_v
        self.log.print_log(
            f'就地升级{befor_v}~{middle_v}~{after_v}再回滚到{middle_v}')
        self.indirect_rollback(self.u_prefix_1, self.u_prefix_2,
                               low_path, high_path, middle_v,
                               mid_path)

    def run_indirect_force_rollback(self, befor_v, middle_v, after_v,
                                    low_path, mid_path, high_path):
        self.up.befor_version = befor_v
        self.up.after_version = after_v
        self.log.print_log(
            f'就地升级{befor_v}~{middle_v}~{after_v}再强制回滚到{middle_v}')
        self.indirect_force_rollback(self.u_prefix_1, self.u_prefix_2,
                                     low_path, high_path, middle_v,
                                     mid_path)

    def run_indirect_grey_cm_cm_cm(self, befor_v, middle_v, after_v, low_path,
                                   mid_path, high_path):
        self.up.befor_version = befor_v
        self.up.after_version = after_v
        self.log.print_log(f'灰度升级CM{befor_v}~CM{middle_v}~CM{after_v}')
        self.indirect_grey_upgrade_cm_cm_cm(self.u_prefix_1, self.u_prefix_2,
                                            low_path, high_path, middle_v,
                                            mid_path)

    def run_indirect_grey_ncm_cm_cm(self, befor_v, middle_v, after_v,
                                    low_path, mid_path, high_path):
        self.up.befor_version = befor_v
        self.up.after_version = after_v
        self.log.print_log(f'灰度升级NCM{befor_v}~CM{middle_v}~CM{after_v}')
        self.indirect_grey_upgrade_ncm_cm_cm(self.u_prefix_1, self.u_prefix_2,
                                             low_path, high_path, middle_v,
                                             mid_path)

    def run_indirect_grey_ncm_ncm_cm(self, befor_v, middle_v, after_v,
                                     low_path, mid_path, high_path):
        self.up.befor_version = befor_v
        self.up.after_version = after_v
        self.log.print_log(f'灰度升级NCM{befor_v}~NCM{middle_v}~CM{after_v}')
        self.indirect_grey_upgrade_ncm_ncm_cm(self.u_prefix_1,
                                              self.u_prefix_2,
                                              low_path,
                                              high_path,
                                              middle_v,
                                              mid_path)


if __name__ == '__main__':
    if len(sys.argv) < 21:
        print(sys.argv)
        raise Exception('请检查入参数量！')
    up_params = dict()
    up_params['userpasswd'] = sys.argv[1]
    up_params['upgrade_ftp_path'] = sys.argv[2]
    up_params['upgrade_user_prefix'] = sys.argv[3] + '_1_'
    up_params['prefixPort'] = sys.argv[4]
    up_params['db_root_path'] = sys.argv[5]
    up_params['pkg_root_path'] = sys.argv[6]
    up_params['ipaddress'] = sys.argv[7]
    up_params['ipaddress_std1'] = sys.argv[8]
    up_params['ipaddress_std2'] = sys.argv[9]
    up_params['befor_version'] = ''
    up_params['after_version'] = ''
    up_params['tpcc_path'] = sys.argv[19]
    hostnames = (sys.argv[10], sys.argv[11], sys.argv[12])
    up = RunUpgrade(*hostnames, **up_params)
    low_v = sys.argv[13]
    mid_v = sys.argv[14]
    high_v = sys.argv[15]
    low_p = sys.argv[16]
    mid_p = sys.argv[17]
    high_p = sys.argv[18]
    jenkins_job_id = sys.argv[20]
    succ_dir = os.path.join(os.getcwd(), 'up_indirect_log', jenkins_job_id,
                            'success_log')
    err_dir = os.path.join(os.getcwd(), 'up_indirect_log', jenkins_job_id,
                           'error_log')
    exec_name = ""

    cmd = f'rm -rf log/*; ' \
        f'mkdir -p {succ_dir}; ' \
        f'mkdir -p {err_dir}'
    subprocess.getoutput(cmd)

    try:
        exec_name = f"灰度升级{low_v}~{mid_v}~{high_v}"
        up.run_indirect_grey(low_v, mid_v, high_v, low_p, mid_p, high_p)
    except Exception as e:
        up.log.print_log(f"{exec_name}失败")
        up.log.print_log(str(e))
        cmd = f'mv log {os.path.join(err_dir, exec_name)}'
        subprocess.getoutput(cmd)
    else:
        up.log.print_log(f"{exec_name}成功")
        cmd = f'mv log {os.path.join(succ_dir, exec_name)}'
        subprocess.getoutput(cmd)

    try:
        exec_name = f"就地升级{low_v}~{mid_v}~{high_v}"
        up.run_indirect(low_v, mid_v, high_v, low_p, mid_p, high_p)
    except Exception as e:
        up.log.print_log(f"{exec_name}失败")
        up.log.print_log(str(e))
        cmd = f'mv log {os.path.join(err_dir, exec_name)}'
        subprocess.getoutput(cmd)
    else:
        up.log.print_log(f"{exec_name}成功")
        cmd = f'mv log {os.path.join(succ_dir, exec_name)}'
        subprocess.getoutput(cmd)

    try:
        exec_name = f"灰度升级{low_v}~{mid_v}~{high_v}再回滚到{mid_v}"
        up.run_indirect_grey_rollback(low_v, mid_v, high_v, low_p, mid_p,
                                      high_p)
    except Exception as e:
        up.log.print_log(f"{exec_name}失败")
        up.log.print_log(str(e))
        cmd = f'mv log {os.path.join(err_dir, exec_name)}'
        subprocess.getoutput(cmd)
    else:
        up.log.print_log(f"{exec_name}成功")
        cmd = f'mv log {os.path.join(succ_dir, exec_name)}'
        subprocess.getoutput(cmd)

    try:
        exec_name = f"灰度升级{low_v}~{mid_v}~{high_v}再强制回滚到{mid_v}"
        up.run_indirect_grey_force_rollback(low_v, mid_v, high_v, low_p,
                                            mid_p, high_p)
    except Exception as e:
        up.log.print_log(f"{exec_name}失败")
        up.log.print_log(str(e))
        cmd = f'mv log {os.path.join(err_dir, exec_name)}'
        subprocess.getoutput(cmd)
    else:
        up.log.print_log(f"{exec_name}成功")
        cmd = f'mv log {os.path.join(succ_dir, exec_name)}'
        subprocess.getoutput(cmd)

    try:
        exec_name = f"就地升级{low_v}~{mid_v}~{high_v}再回滚到{mid_v}"
        up.run_indirect_rollback(low_v, mid_v, high_v, low_p, mid_p, high_p)
    except Exception as e:
        up.log.print_log(f"{exec_name}失败")
        up.log.print_log(str(e))
        cmd = f'mv log {os.path.join(err_dir, exec_name)}'
        subprocess.getoutput(cmd)
    else:
        up.log.print_log(f"{exec_name}成功")
        cmd = f'mv log {os.path.join(succ_dir, exec_name)}'
        subprocess.getoutput(cmd)

    try:
        exec_name = f"就地升级{low_v}~{mid_v}~{high_v}再强制回滚到{mid_v}"
        up.run_indirect_force_rollback(low_v, mid_v, high_v, low_p, mid_p,
                                       high_p)
    except Exception as e:
        up.log.print_log(f"{exec_name}失败")
        up.log.print_log(str(e))
        cmd = f'mv log {os.path.join(err_dir, exec_name)}'
        subprocess.getoutput(cmd)
    else:
        up.log.print_log(f"{exec_name}成功")
        cmd = f'mv log {os.path.join(succ_dir, exec_name)}'
        subprocess.getoutput(cmd)

    try:
        exec_name = f"灰度升级CM{low_v}~CM{mid_v}~CM{high_v}"
        up.run_indirect_grey_cm_cm_cm(low_v, mid_v, high_v, low_p, mid_p,
                                      high_p)
    except Exception as e:
        up.log.print_log(f"{exec_name}失败")
        up.log.print_log(str(e))
        cmd = f'mv log {os.path.join(err_dir, exec_name)}'
        subprocess.getoutput(cmd)
    else:
        up.log.print_log(f"{exec_name}成功")
        cmd = f'mv log {os.path.join(succ_dir, exec_name)}'
        subprocess.getoutput(cmd)

    try:
        exec_name = f"灰度升级NCM{low_v}~CM{mid_v}~CM{high_v}"
        up.run_indirect_grey_ncm_cm_cm(low_v, mid_v, high_v, low_p, mid_p,
                                       high_p)
    except Exception as e:
        up.log.print_log(f"{exec_name}失败")
        up.log.print_log(str(e))
        cmd = f'mv log {os.path.join(err_dir, exec_name)}'
        subprocess.getoutput(cmd)
    else:
        up.log.print_log(f"{exec_name}成功")
        cmd = f'mv log {os.path.join(succ_dir, exec_name)}'
        subprocess.getoutput(cmd)

    try:
        exec_name = f"灰度升级NCM{low_v}~NCM{mid_v}~CM{high_v}"
        up.run_indirect_grey_ncm_ncm_cm(low_v, mid_v, high_v, low_p, mid_p,
                                        high_p)
    except Exception as e:
        up.log.print_log(f"{exec_name}失败")
        up.log.print_log(str(e))
        cmd = f'mv log {os.path.join(err_dir, exec_name)}'
        subprocess.getoutput(cmd)
    else:
        up.log.print_log(f"{exec_name}成功")
        cmd = f'mv log {os.path.join(succ_dir, exec_name)}'
        subprocess.getoutput(cmd)
